<template>
	<view class="container">
		<!-- 地图部分 -->
		<view class="page-map">
			<map class="map-content" :latitude="latitude" :longitude="longitude" markers="markers"></map>
		</view>
		<!-- 门店信息 -->
		<view class="content">
			<view class="store" v-for="(store, index) in storeData" :key="index" @tap="storeTap(store)">
				<view class="store-left">
					<view class="store-title">
						<view class="store-name">{{store.name}}</view>
						<view class="store-status">营业中</view>
					</view>
					<view class="store-content">
						<view class="distance">{{store.distance}}</view>
						<view class="store-text">{{store.street}}</view>
						<view class="store-text">营业时间10:00 - 22:00</view>
					</view>
				</view>
				<view class="store-right">
					<view class="look">去看看</view>
					<view class="icon">
						<view class="round">
							<image src="../../static/stores/store-tel.png" ></image>
						</view>
						<view class="round">
							<image src="../../static/stores/store-nav.png"></image>
						</view>
					</view>
			</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	export default {
		data() {
			return {
				latitude: '',
				longitude: '',
				markers: [],
				storeData: []
			}
		},
		onLoad() {
			this.getStoreData()
		},
		methods: {
			...mapMutations(['SET_ORDER_TYPE', 'SET_STORE']),
			getStoreData() {
				const vm =this
				return new Promise((resolve, reject) => {
					uni.getLocation({
						type: 'gcj02',
						success(resLocation){
							let {latitude, longitude} = resLocation
							if(latitude && longitude) {
								resolve(resLocation)
							}
						}
					})
				}).then(local => {
					let la1 = local.latitude
					let lo1 = local.longitude
					return uniCloud.callFunction({
						name:'stores'
					}).then(resStores => {
						console.log(resStores)
						let temp = []
						let l = resStores.result.data.length
						if(l>=1) {
							for(let i=0;i<l;i++) {
								let element = resStores.result.data[i]
								let d = vm.distance(element.latitude, element.longitude, la1, lo1)
								element.distance = d
								temp.push(element)
							}
							vm.storeData = temp.sort((a,b) => {
								return a.distance - b.distance
							})
							return vm.storeData
						}
					}).then (resMap => {
						vm.latitude = resMap[0].latitude
						vm.longitude = resMap[0].longitude
						let map = []
						let len = resMap.length
						if(len >= 1) {
							for(let i=0; i<len; i++) {
								let maps = {}
								maps.id = i
								maps.latitude = resMap[i].latitude
								maps.longitude = resMap[i].longitude
								maps.iconPath = '/statuc/images/logo.png'
								maps.width = 30
								maps.height = 30
								maps.callout = {'content': resMap[i].name, 'display': 'ALWAYS'}
								map.push(maps)
							}
							vm.markers = map
						}
					})
				})
			},
			distance(la1, lo1, la2, lo2) {
				let La1 =la1 * Math.PI /180.0;
				let La2 =la2 * Math.PI /180.0;
				let La3 = La1 - la2
				let Lb3 = lo1 * Math.PI /180.0 - lo2 * Math.PI / 180.0
				let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3/2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
				s = s * 6378.137;
				s = Math.round(s * 10000) / 10000;
				s = s.toFixed(1)
				return s
			},
			storeTap(store) {
				this.SET_ORDER_TYPE('takein')
				console.log('store', store)
				this.SET_STORE(store)
				uni.switchTab({
					url:'/pages/menu/menu'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page-map {
		width: 100%;
		height: 400rpx;
	}
	.map-content {
		width: 100%;
		height: 400rpx;
	}
	.content {
		width: 100%;
		// height: 100%;
		display: flex;
	}
	.store {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		margin: 10rpx 20rpx;
		padding: 20rpx;
		background: #fff;
		box-shadow: $box-shadow;
	}
	.store-left {
		width: 70%;
		display: flex;
		flex-direction: column;
		.store-title {
			display: flex;
			flex-wrap: nowrap;
			.store-name {
				color: $text-color-black;
				font-size: $font-size-lg;
				font-weight: bold;
				margin-right: 50rpx;
			}
			.store-status {
				color: $color-primary;
				font-size: $font-size-sm;
				border: 1rpx solid $color-primary;
				padding: 0 5rpx;
			}
		}
		.store-content {
			margin-top: 60rpx;
			font-size: $font-size-sm;
			display: flex;
			flex-direction: column;
			.store-text {
				color: $text-color-assist;
				white-space: nowrap;
				text-overflow: ellipsis;
				overflow: hidden;
			}
		}
	}
	.store-right {
		width: 30%;
		display: flex;
		flex-direction: column;
		.look {
			color: $color-primary;
			font-size: $font-size-sm;
			text-align: end;
		}
		.icon {
			height: 100%;
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			justify-content: flex-end;
			.round {
				border-radius: 50%;
				width: 60rpx;
				height: 60rpx;
				margin: 10rpx;
				background-color: #eee;
				image {
					display: block;
					width: 40rpx;
					height: 40rpx;
					margin:10rpx;
				}
			}
		}
	}
</style>
